﻿using log4net;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;

namespace HobSoft.SoccerBet2014.Client.ViewModels
{
    using Common;
    public abstract class MessageViewModel : ViewModelBase
    {
        [NonSerialized]
        private ILog _publicLog;
        [NonSerialized]
        private ILog _errorLog;
        [NonSerialized]
        private ILog _emptyLog;
        protected ILog PublicLog
        {
            get
            {
                if (_publicLog == null)
                    return _emptyLog;
                return _publicLog;
            }
        }
        protected ILog ErrorLog
        {
            get
            {
                if (_errorLog == null)
                    return _emptyLog;
                return _errorLog;
            }
        }

        protected override void OnInitializeOver()
        {
            _publicLog = LogManager.GetLogger("ApplicationCacheLog");
            _errorLog = LogManager.GetLogger("ErrorLoger");
            _emptyLog = new Log.EmptyLog();
            messageModels = new ObservableCollection<ModelMessage>();
            ModelMessages = new ReadOnlyObservableCollection<ModelMessage>(messageModels);
            OnInitializeMessageOver();
        }

        public virtual void OnInitializeMessageOver()
        {

        }

        #region warn info
        [NonSerialized]
        private ObservableCollection<ModelMessage> messageModels;
        [NonSerialized]
        private ReadOnlyObservableCollection<ModelMessage> _messageModels;

        public ReadOnlyObservableCollection<ModelMessage> ModelMessages
        {
            get { return _messageModels; }
            set
            {
                _messageModels = value;
                RaisePropertyChanged("ModelMessages");
            }
        }

        public bool HasMessage
        {
            get
            {
                return _messageModels.Any();
            }
        }

        public ModelMessage NewMessage
        {
            get
            {
                if (!HasMessage) return null;
                return ModelMessages.LastOrDefault();
            }
        }

        protected void RaiseMessage(string source, string message)
        {
            if (messageModels == null)
            {
                messageModels = new ObservableCollection<ModelMessage>();
                ModelMessages = new ReadOnlyObservableCollection<ModelMessage>(messageModels);
            }
            ModelMessage warn = new ModelMessage() { SourceName = source, Message = message, SendTime = DateTime.Now };
            messageModels.Add(warn);
            RaisePropertyChanged("NewMessage");
            RaisePropertyChanged("HasMessage");
        }

        protected void RaiseMessage(string source, string message, int clearTicks)
        {
            RaiseMessage(source, message);
            Task.Factory.StartNew(() =>
            {
                Task.Delay(clearTicks).ContinueWith(t =>
                {
                    BetApplication.Current.Dispatcher.Invoke(() =>
                    {
                        ClearMessage();
                    });
                });
            });
        }

        protected void ClearMessage()
        {
            messageModels.Clear();
            RaisePropertyChanged("NewMessage");
            RaisePropertyChanged("HasMessage");
        }

        protected void ClearMessage(Func<ModelMessage, bool> predicate)
        {
            var array = messageModels.Where(predicate).ToArray();
            for (int i = 0; i < array.Length; i++)
            {
                messageModels.Remove(array[i]);
            }
            if (!messageModels.Any())
            {
                RaisePropertyChanged("NewMessage");
                RaisePropertyChanged("HasMessage");
            }
        }
        #endregion
    }
}
